
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Service · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="ContentProvider.html" />
    
    
    <link rel="prev" href="Activity.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../">
            
                <a href="../">
            
                    
                    Android
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../Activity.md">
            
                <span>
            
                    
                    四大组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.1" data-path="Activity.html">
            
                <a href="Activity.html">
            
                    
                    Activity
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.1.2" data-path="Service.html">
            
                <a href="Service.html">
            
                    
                    Service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.3" data-path="ContentProvider.html">
            
                <a href="ContentProvider.html">
            
                    
                    ContentProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.4" data-path="BroadcastReceiver.html">
            
                <a href="BroadcastReceiver.html">
            
                    
                    BroadcastReceiver
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../Fragment.html">
            
                <a href="../Fragment.html">
            
                    
                    Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../Context.html">
            
                <a href="../Context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../View.html">
            
                <a href="../View.html">
            
                    
                    View
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../动画.html">
            
                <a href="../动画.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../WebView.html">
            
                <a href="../WebView.html">
            
                    
                    WebView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../ListView与RecyclerView.html">
            
                <a href="../ListView与RecyclerView.html">
            
                    
                    ListView与RecyclerView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../线程.html">
            
                <a href="../线程.html">
            
                    
                    线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../进程.html">
            
                <a href="../进程.html">
            
                    
                    进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../序列化.html">
            
                <a href="../序列化.html">
            
                    
                    序列化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../图片加载与Bitmap.html">
            
                <a href="../图片加载与Bitmap.html">
            
                    
                    图片加载与Bitmap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../内存泄漏与内存溢出.html">
            
                <a href="../内存泄漏与内存溢出.html">
            
                    
                    内存泄漏与内存溢出
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="../ANR.html">
            
                <a href="../ANR.html">
            
                    
                    ANR
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="../SparseArray.html">
            
                <a href="../SparseArray.html">
            
                    
                    SparseArray
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="../网络.html">
            
                <a href="../网络.html">
            
                    
                    网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../性能优化.html">
            
                <a href="../性能优化.html">
            
                    
                    性能优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.17" data-path="../适配.html">
            
                <a href="../适配.html">
            
                    
                    适配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.18" data-path="../架构设计.html">
            
                <a href="../架构设计.html">
            
                    
                    架构设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.19" data-path="../Java虚拟机与Dalvik、ART虚拟机的区别.html">
            
                <a href="../Java虚拟机与Dalvik、ART虚拟机的区别.html">
            
                    
                    Java虚拟机与Dalvik、ART虚拟机的区别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.20" data-path="../ActivityThread、AMS、WMS的工作原理.html">
            
                <a href="../ActivityThread、AMS、WMS的工作原理.html">
            
                    
                    ActivityThread、AMS、WMS的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.21" data-path="../混合开发.html">
            
                <a href="../混合开发.html">
            
                    
                    混合开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.22" data-path="../插件化.html">
            
                <a href="../插件化.html">
            
                    
                    插件化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.23" data-path="../第三方库.html">
            
                <a href="../第三方库.html">
            
                    
                    第三方库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../Java/REAM.html">
            
                <a href="../../Java/REAM.html">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../../Java/Java基础.html">
            
                <a href="../../Java/Java基础.html">
            
                    
                    Java基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../../Java/JVM.html">
            
                <a href="../../Java/JVM.html">
            
                    
                    JVM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../../Java/数据结构.html">
            
                <a href="../../Java/数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../../Java/多线程.html">
            
                <a href="../../Java/多线程.html">
            
                    
                    多线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="../../Java/应用类型的区别与使用场景.html">
            
                <a href="../../Java/应用类型的区别与使用场景.html">
            
                    
                    引用类型的区别与使用场景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="../../Java/异常处理.html">
            
                <a href="../../Java/异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="../../Java/设计模式.html">
            
                <a href="../../Java/设计模式.html">
            
                    
                    设计模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.8" data-path="../../Java/算法.html">
            
                <a href="../../Java/算法.html">
            
                    
                    算法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../Http/">
            
                <a href="../../Http/">
            
                    
                    网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../../Http/网络分层.html">
            
                <a href="../../Http/网络分层.html">
            
                    
                    网络分层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../../Http/http.html">
            
                <a href="../../Http/http.html">
            
                    
                    Http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../../Http/Https.html">
            
                <a href="../../Http/Https.html">
            
                    
                    Https
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../../Http/TCP.html">
            
                <a href="../../Http/TCP.html">
            
                    
                    TCP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../../Http/Socket.html">
            
                <a href="../../Http/Socket.html">
            
                    
                    Socket
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Service</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="service">Service</h1>
<h3 id="service-&#x751F;&#x547D;&#x5468;&#x671F;">Service &#x751F;&#x547D;&#x5468;&#x671F;</h3>
<p><img src="pic/Service&#x751F;&#x547D;&#x5468;&#x671F;.jpg" alt=""></p>
<h3 id="startservice-&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;">startService &#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;</h3>
<p><img src="pic/startService&#x7684;&#x6D41;&#x7A0B;.jpg" alt=""></p>
<h3 id="bindservice-&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;">bindService &#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;</h3>
<p><img src="pic/bindService&#x7684;&#x6D41;&#x7A0B;.jpg" alt=""></p>
<h3 id="stopservice-&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;">stopService &#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;</h3>
<p><img src="pic/stopService&#x7684;&#x6D41;&#x7A0B;.jpg" alt=""></p>
<h3 id="unbindservice-&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;">unbindService &#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x6D41;&#x7A0B;</h3>
<p><img src="pic/unbindService&#x7684;&#x6D41;&#x7A0B;.jpg" alt=""></p>
<h3 id="service-&#x7684;&#x5F00;&#x542F;&#x65B9;&#x5F0F;">Service &#x7684;&#x5F00;&#x542F;&#x65B9;&#x5F0F;</h3>
<ul>
<li><p>&#x901A;&#x8FC7; startService</p>
<p>&#x521B;&#x5EFA; Service</p>
<p>&#x5728; AndroidManifest &#x4E2D;&#x6CE8;&#x518C; Service</p>
<p>&#x901A;&#x8FC7; startService &#x65B9;&#x6CD5;&#x542F;&#x52A8; Service</p>
</li>
</ul>
<pre><code class="lang-java">Intent intent = <span class="hljs-keyword">new</span> Intent(MainActivity.<span class="hljs-keyword">this</span>, TestService.class);
startService(intent);
</code></pre>
<ul>
<li>&#x901A;&#x8FC7; bindService</li>
</ul>
<h3 id="activity-&#x600E;&#x4E48;&#x548C;-service-&#x7ED1;&#x5B9A;">Activity &#x600E;&#x4E48;&#x548C; Service &#x7ED1;&#x5B9A;</h3>
<p>&#x901A;&#x8FC7; bindService &#x65B9;&#x6CD5;</p>
<pre><code class="lang-java">ServiceConnection serviceConnection = <span class="hljs-keyword">new</span> ServiceConnection() {
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onServiceConnected</span><span class="hljs-params">(ComponentName name, IBinder service)</span> </span>{
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onServiceDisconnected</span><span class="hljs-params">(ComponentName name)</span> </span>{
    }
};

bindService(intent, serviceConnection, BIND_AUTO_CREATE);
startService(intent);
</code></pre>
<h3 id="service-&#x548C;-activity-&#x600E;&#x4E48;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x4EA4;&#x4E92;">Service &#x548C; Activity &#x600E;&#x4E48;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x4EA4;&#x4E92;</h3>
<ul>
<li>&#x901A;&#x8FC7;&#x5E7F;&#x64AD;</li>
<li>&#x901A;&#x8FC7;&#x5171;&#x4EAB;&#x6587;&#x4EF6;</li>
<li>&#x901A;&#x8FC7; AIDL</li>
<li><p>&#x901A;&#x8FC7; Messenger</p>
</li>
<li><p>&#x901A;&#x8FC7;&#x81EA;&#x5B9A;&#x4E49; Binder &#x5E76;&#x4F7F;&#x7528; bindService</p>
<p>&#x521B;&#x5EFA; Service&#x3001;&#x81EA;&#x5B9A;&#x4E49; Binder&#xFF0C;&#x5E76;&#x5728; onBind &#x65B9;&#x6CD5;&#x5C06;&#x81EA;&#x5B9A;&#x4E49; Binder &#x8FD4;&#x56DE;&#x5728; Activity &#x4E2D;&#x4F7F;&#x7528;</p>
</li>
</ul>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Service</span> </span>{

    MyBinder myBinder;
    DoSomethingListener doSomethingListener;

    <span class="hljs-meta">@Nullable</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> IBinder <span class="hljs-title">onBind</span><span class="hljs-params">(Intent intent)</span> </span>{

        myBinder = <span class="hljs-keyword">new</span> MyBinder();
        <span class="hljs-keyword">return</span> myBinder;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doSomeThing</span><span class="hljs-params">(String str)</span> </span>{

        <span class="hljs-keyword">if</span> (doSomethingListener != <span class="hljs-keyword">null</span>) {
            doSomethingListener.onDoSomething(str);
        }

    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setDoSomethingListener</span><span class="hljs-params">(DoSomethingListener listener)</span> </span>{
        doSomethingListener = listener;
    }

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyBinder</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Binder</span> </span>{

        <span class="hljs-function"><span class="hljs-keyword">public</span> TestService <span class="hljs-title">getService</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">return</span> TestService.<span class="hljs-keyword">this</span>;
        }

    }

    <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">DoSomethingListener</span> </span>{

        <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">onDoSomething</span><span class="hljs-params">(String str)</span></span>;

    }

}
</code></pre>
<p>ServiceConnection &#x4E0E; Service &#x7ED1;&#x5B9A;&#xFF0C;&#x5E76;&#x4ECE;&#x8FD4;&#x56DE;&#x7684; Binder &#x4E2D;&#x53D6;&#x51FA; Service &#x5E76;&#x4FDD;&#x5B58;&#x3002;&#x7136;&#x540E;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x4E0E; Service &#x4EA4;&#x4E92;</p>
<pre><code>ServiceConnection serviceConnection;
    TestService testService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder iBinder) {
                testService = ((TestService.MyBinder) iBinder).getService();
                testService.setDoSomethingListener(new         TestService.DoSomethingListener() {
                    @Override
                    public void onDoSomething(String str) {
                        Log.e(&quot;borax-doSomething&quot;, str);
                    }
                });
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };

        Intent intent = new Intent(MainActivity.this, TestService.class);
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);

        testService.doSomeThing(&quot;Do Something~~~&quot;);
    }
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Activity.html" class="navigation navigation-prev " aria-label="Previous page: Activity">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="ContentProvider.html" class="navigation navigation-next " aria-label="Next page: ContentProvider">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Service","level":"1.2.1.2","depth":3,"next":{"title":"ContentProvider","level":"1.2.1.3","depth":3,"path":"Android/四大组件/ContentProvider.md","ref":"Android/四大组件/ContentProvider.md","articles":[]},"previous":{"title":"Activity","level":"1.2.1.1","depth":3,"path":"Android/四大组件/Activity.md","ref":"Android/四大组件/Activity.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"Android/四大组件/Service.md","mtime":"2019-01-21T04:06:33.758Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-01-21T14:04:13.553Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

